<?php

// +----------------------------------------------------------------------
// | LuShop [ Lulu COMPANY WEB SHOW]
// +----------------------------------------------------------------------
// | Copyright (c) 2010 http://www.luluui.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: uuleaf <uuleaf@163.com>
// +----------------------------------------------------------------------
class OrderformAction extends MbaseAction {
	public $tbname = 'Orderform';
	public $obj = '';
	public $mpk = 'orderform_id';
	public function unionindex() {
		$this->index();
	}
	public function view() {
		$mpk = isset ($_GET['orderform_id']) ? $_GET['orderform_id'] : 0;
		$mof = D('Orderform');
		$mof->autoDeal($mpk);
		$mgood = M('Good');
		$datainfo = $mof->relation(true)->find($mpk);
		if ($datainfo['good']) {
			foreach ($datainfo['good'] as $key => $agood) {
				$tgood = $mgood->find($agood['good_id']);
				if ($tgood)
					$datainfo['good'][$key] = array_merge($datainfo['good'][$key], $tgood);
			}
		}
		$mcity = M('Citys');
		$cmap['nid'] = $datainfo['province'];
		$address = $mcity->where($cmap)->getField('name');
		$cmap['nid'] = $datainfo['city'];
		$address .= $mcity->where($cmap)->getField('name');
		$cmap['nid'] = $datainfo['county'];
		$address .= $mcity->where($cmap)->getField('name');
		$datainfo['addr'] = $address;
		$this->assign('datainfo', $datainfo);
		$this->display();
	}
	//修改付款方式
	public function pay_type() {
		$mpk = isset ($_POST['orderform_id']) ? $_POST['orderform_id'] : '';
		$pay_type = isset ($_POST['pay_type']) ? $_POST['pay_type'] : '';
		if ($mpk == '' || $pay_type == '')
			$this->error('参数错误', 1);
		$map['orderform_id'] = $mpk;
		$mof = D('Orderform');
		if ($mof->where($map)->setField('pay_type', $pay_type)) {
			$this->setReload('navtab','Orderform_'.$mpk);
			$this->success('成功修改付款方式。', 1);
		} else {
			$this->error('参数错误', 1);
		}
	}
	//已经付款2
	public function pay() {
		$mpk = isset ($_POST['orderform_id']) ? $_POST['orderform_id'] : 0;
		if ($mpk == 0)
			$this->error('参数错误', 1);
		$map['orderform_id'] = $mpk;
		$mof = D('Orderform');
		if ($mof->where($map)->setField(array (
				'progress',
				'pay_time'
			), array (
				2,
				time()
			))) {
			$this->do_order_point($mpk, 2);
			$this->setReload('navtab','Orderform_'.$mpk);
			$this->success('交易已成功付款。', 1);
		} else {
			$this->error('参数错误', 1);
		}
	}
	//发货3
	public function delivery() {
		$mpk = isset ($_POST['orderform_id']) ? $_POST['orderform_id'] : 0;
		$express_type = isset ($_POST['express_type']) ? $_POST['express_type'] : '';
		$express_code = isset ($_POST['express_code']) ? $_POST['express_code'] : '';
		if ($mpk == 0) {
			$this->error('参数错误', 1);
			return false;
		}
		if ($express_type == '') {
			$this->error('请选择快递方式', 1);
			return false;
		}
		if ($express_code == '') {
			$this->error('请填写快递运单号', 1);
			return false;
		}
		$map['orderform_id'] = $mpk;
		$mof = D('Orderform');
		if ($mof->where($map)->setField(array (
				'express_type',
				'express_code',
				'progress',
				'delivery_time'
			), array (
				$express_type,
				$express_code,
				3,
				time()
			))) {
			$this->setReload('navtab','Orderform_'.$mpk);
			$this->success('交易已成功付款。', 1);
		} else {
			$this->error('参数错误', 1);
		}
	}
	//成功交易4
	public function complete() {
		$mpk = isset ($_POST['orderform_id']) ? $_POST['orderform_id'] : 0;
		if ($mpk == 0)
			$this->error('参数错误', 1);
		$map['orderform_id'] = $mpk;
		$mof = D('Orderform');
		if ($mof->where($map)->setField(array (
				'progress',
				'complete_time'
			), array (
				4,
				time()
			))) {
			$this->do_good_order($mpk);
			$this->do_order_point($mpk, 4);
			$this->setReload('navtab','Orderform_'.$mpk);
			$this->success('交易已成功。', 1);
		} else {
			$this->error('参数错误', 1);
		}
	}
	//关闭订单5
	public function turnoff() {
		$mpk = isset ($_POST['orderform_id']) ? $_POST['orderform_id'] : 0;
		if ($mpk == 0)
			$this->error('参数错误', 1);
		$map['orderform_id'] = $mpk;
		$mof = D('Orderform');
		if ($mof->where($map)->setField(array (
				'progress',
				'turnoff_time'
			), array (
				5,
				time()
			))) {
			$this->setReload('navtab','Orderform_'.$mpk);
			$this->success('交易已成功关闭。', 1);
		} else {
			$this->error('参数错误', 1);
		}
	}
	//退款6
	public function cancel() {
		$mpk = isset ($_POST['orderform_id']) ? $_POST['orderform_id'] : 0;
		if ($mpk == 0)
			$this->error('参数错误', 1);
		$map['orderform_id'] = $mpk;
		$mof = D('Orderform');
		if ($mof->where($map)->setField(array (
				'progress',
				'cancel_time'
			), array (
				6,
				time()
			))) {
			$this->setReload('navtab','Orderform_'.$mpk);
			$this->success('交易已设置为退款中。', 1);
		} else {
			$this->error('参数错误', 1);
		}
	}
	//RESTET
	public function turnon() {
		$mpk = isset ($_POST['orderform_id']) ? $_POST['orderform_id'] : 0;
		if ($mpk == 0)
			$this->error('参数错误', 1);
		$map['orderform_id'] = $mpk;
		$mof = D('Orderform');
		if ($mof->where($map)->setField(array (
				'progress',
				'cancel_time'
			), array (
				1,
				time()
			))) {
			$this->success('交易已设置为退款中。', 1);
		} else {
			$this->error('参数错误', 1);
		}
	}
	//每一个订单完成，激活商品购买情况
	public function do_good_order($mpk) {
		$omap['orderform_id'] = $mpk;
		$mmember = M('Member');
		$mgood = M('Good');
		$mgood_order = M('Good_order');
		$mof = D('Orderform');
		$orderinfo = $mof->where($omap)->relation('good')->find();
		$mmap['member_id'] = $orderinfo['member_id'];
		$member_name = $mmember->where($mmap)->getField('username');
		foreach ($orderinfo['good'] as $agood) {
			$datainfo = array (
				'member_id' => $orderinfo['member_id'],
				'member_name' => $member_name,
				'good_id' => $agood['good_id'],
				'good_name' => $agood['good_name'],
				'good_price' => $agood['good_price'],
				'good_num' => $agood['good_num'],
				'dealtime' => time(),
				'state' => 4,
				
			);
			$mgood_order->add($datainfo);
			$gmap['good_id'] = $agood['good_id'];
			$mgood->setInc('order_count', $gmap);
		}
	}

	//每一个定单完成 后处理积分
	//$t=2 扣分 $t=4 赠分
	public function do_order_point($mpk, $t = 0) {
		$morder_point = M('Orderform_point');
		$map['orderform_id'] = $mpk;
		$pointinfo = $morder_point->where($map)->find();
		if ($pointinfo) {
			$mmem_point = D('Member_points');
			$rsinfo = $mmem_point->deal_orderform($pointinfo['member_id'], $pointinfo['fee'], $t);
			if ($t == 4) {
				$morder_point->where($map)->setField(array (
					'get_point',
					'dis_point',
					'now_point',
					'dis_fee',
					'happen',
					'is_ok'
				), array (
					$rsinfo['get_point'],
					$rsinfo['dis_point'],
					$rsinfo['now_point'],
					$rsinfo['dis_fee'],
					time(),
					1
				));

				$rmap['member_id'] = $pointinfo['member_id'];
				$rmap['is_ok'] = 0;
				$discount = $mmem_point->get_discount($pointinfo['member_id'], 0);
				$morder_point->where($rmap)->setField(array (
					'nowpoint',
					'dis_point'
				), $rsinfo['now_point'], $discount);

			}

			$morderform = M('Orderform');
			$omap['member_id'] = $pointinfo['member_id'];
			$omap['progress'] = array (
				'eq',
				1
			);
			$olist = $morderform->where($omap)->field('orderform_id,freight_fee,total_fee')->select();
			if ($olist) {
				foreach ($olist as $vo) {
					$ormap['orderform_id'] = $vo['orderform_id'];
					if ($t == 2)
						$discount = 0;
					$fee = $vo['freight_fee'] + $vo['total_fee'] - $discount;
					$morderform->where($ormap)->setField(array (
						'discount_fee',
						'final_fee'
					), array (
						$discount,
						$fee
					));
				}

			}

		}

	}

	public function addnote() {
		$mpk = isset ($_POST['orderform_id']) ? $_POST['orderform_id'] : '';
		$note = isset ($_POST['note']) ? $_POST['note'] : '';
		if ($mpk == '' || $note == '')
			$this->error('参数错误', 1);
		$mnote = M('Orderform_note');
		$notearr = array (
			'orderform_id' => $mpk,
			'note' => $note,
			'created' => time(),
			
		);
		if (!$mnote->add($notearr)) {
			$this->error($mnote->getError(), 1);
			return false;
		}
		$this->setForward(U('view?orderform_id=' . $mpk));
		$this->success('备注成功', 1);
	}
	public function delnote() {
		$mpk = isset ($_POST['id']) ? $_POST['id'] : 0;
		if ($mpk == 0)
			$this->error('参数错误', 1);
		$mnote = M('Orderform_note');
		if (!$mnote->where('id=' . $mpk)->delete()) {
			$this->error($mnote->getError(), 1);
			return false;
		}
		$this->setForward(U('view?orderform_id=' . $_POST['orderform_id']));
		$this->success('删除备注成功', 1);
	}
}
?>